<template>
	<view class="content">
		<view>
			举报人姓名
			<text class="huang">※</text>
		</view>
		<input type="text" v-model="reportuser" placeholder="请输入姓名" />
		<view>
			举报人手机号
			<text class="huang">※</text>
		</view>
		<input type="number" v-model="phone" placeholder="请输入手机号" />
		<view>
			举报描述
			<text class="huang">※</text>
		</view>

		<input type="text" v-model="remark" placeholder="请输入描述" />
		<text>位置:{{ address }}</text>
		<view>
			<text>照片:({{ imageList.length }}/4)</text>
			<text style="color: #fcd700;padding-left: 5upx;">至少上传1张</text>
			<view class="uni-uploader">
				<view class="uni-uploader-body">
					<view class="uni-uploader__files" style="width: 100%;display: flex;">
						<block v-if="imageList.length !== 0">
							<view v-for="val of imageList" :key="val.id" class="uni-uploader__file" style="position: relative;width: 30%;margin-right: 20upx;">
								<image class="uni-uploader__img" :src="val.src"></image>
								<view class="close-view" @tap="close(val.id)">x</view>
							</view>
						</block>
						<view class="uni-uploader__input-box" style="width: 30%;margin: 10upx;" v-if="imageList.length < 4">
							<view class="uni-uploader__input" @tap="chooseImg()"></view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<button type="primary" style="background-color:#fcd700;" @tap="chaKong">举报</button>
	</view>
</template>

<script>
import CHONG from '../../static/js/function.js';
import amap from '../../components/amap-wx.js';
import { mapState, mapMutations } from 'vuex';
export default {
	computed: mapState(['URL']),
	data() {
		// 初始值
		return {
			dogid: '',
			reportuser: '',
			phone: '',
			remark: '',
			address: '',

			amapPlugin: null,
			key: '1304da7af930333af8db1a995f4d2527',
			weather: {
				hasData: false,
				data: []
			},
			imageList: [],
			tupian: '',
			i: 0,
			arr: []
			// time: ''
		};
	},
	methods: {
		postJuBao() {
			let data = {
				dogid: this.dogid,
				reportuser: this.reportuser,
				phone: this.phone,
				remark: this.remark,
				address: this.address,
				tupian: this.tupian
			};
			CHONG.tip('提交中', 0);
			console.log(data);
			uni.request({
				url: this.URL + 'reportAdd',
				data: data,
				success: res => {
					if (!res.data.success) {
						CHONG.tip(res.data.msg);
					}
					console.log(res.data);
					CHONG.tip(res.data.msg);
					setTimeout(() => {
						uni.navigateBack({});
					}, 1500);
				}
			});
		},
		chaKong() {
			if (!this.reportuser) {
				CHONG.tip('姓名不能为空');
				return;
			} else if (!this.phone.length) {
				CHONG.tip('手机号不能为空');
				return;
			} else if (this.phone.length!==11) {
				CHONG.tip('手机号格式不正确');
				return;
			} else if (!this.remark) {
				CHONG.tip('描述不能为空');
				return;
			} else if (!this.imageList.length) {
				CHONG.tip('请上传图片');
				return;
			}
			this.upImg();
		},
		upImg() {
			console.log("长传图片");
			uni.uploadFile({
				url: this.URL + 'uploadreport',
				filePath: this.imageList[this.i]['src'],
				name: 'tupian',
				success: uploadFileRes => {
					let json = JSON.parse(uploadFileRes.data);
					this.arr.push(json.data[0].fileName);
					this.i++;
					console.log(json.data[0].fileName);
					console.log(this.i, this.imageList.length);
					if (this.i === this.imageList.length) {
						console.log("图片上传成功");

						let tupian = this.arr.join(',');
						this.tupian = tupian;
						this.postJuBao();
					} else {
						this.upImg();
					}
				}
			});
		},
		getRegeo() {
			CHONG.tip("获取信息中",0)
			this.amapPlugin.getRegeo({
				success: data => {
					console.log(data);
					this.address = data[0].name;
					uni.hideLoading();
				}
			});
		},
		chooseImg() {
			//选择图片
			uni.chooseImage({
				sourceType: ['camera', 'album'],
				count: 4,
				success: res => {
					this.imageList.push({
						src: res.tempFilePaths[0],
						id: res.tempFilePaths[0]
					});
				}
			});
		},
		close(e) {
			let arr = [];
			this.imageList.map((val, index) => {
				if (val.id !== e) {
					arr.push(val);
				}
			});
			this.imageList = arr;
		}
	},
	onLoad(e) {
		this.dogid = e.id;
		this.amapPlugin = new amap.AMapWX({
			key: this.key
		});
		this.getRegeo();
	}
};
</script>

<style lang="scss">
.content {
	font-size: 26upx;
	padding: 20upx;
	.huang {
		color: $chong-iconbg-color;
	}
	input {
		color: $chong-text-color;
		font-size: 24upx;
		margin: 10upx 0;
		padding: 4upx 20upx;
		border: {
			radius: 10upx;
			style: solid;
			width: 1upx;
			color: rgba($chong-text-color, 0.2);
		}
		background: white;
	}
}
/* 上传 */
.uni-uploader {
	flex: 1;
	flex-direction: column;
	width: 100%;
}
.uni-uploader-info {
	color: #b2b2b2;
}
.uni-uploader-body {
	margin-top: 16upx;
}
.uni-uploader__files {
	display: flex;
	flex-direction: row;
	flex-wrap: wrap;
}
.uni-uploader__file {
	width: 208upx;
	width: 30%;
	height: 208upx;
	margin: 10px 0px;
}
.uni-uploader__img {
	display: block;
	width: 208upx;
	height: 208upx;
}
.uni-uploader__input-box {
	position: relative;
	margin: 10upx auto;
	width: 208upx;
	height: 208upx;
	border: 2upx solid #d9d9d9;
}
.uni-uploader__input-box:before,
.uni-uploader__input-box:after {
	content: ' ';
	position: absolute;
	top: 50%;
	left: 50%;
	-webkit-transform: translate(-50%, -50%);
	transform: translate(-50%, -50%);
	background-color: #d9d9d9;
}
.uni-uploader__input-box:before {
	width: 4upx;
	height: 79upx;
}
.uni-uploader__input-box:after {
	width: 79upx;
	height: 4upx;
}
.uni-uploader__input-box:active {
	border-color: #999999;
}
.uni-uploader__input-box:active:before,
.uni-uploader__input-box:active:after {
	background-color: #999999;
}
.uni-uploader__input {
	position: absolute;
	z-index: 1;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	opacity: 0;
}
.uni-uploader__file,
.uni-uploader__input-box {
	width: 50%;
}
.close-view {
	text-align: center;
	line-height: 14px;
	height: 16px;
	width: 16px;
	border-radius: 50%;
	background: #ff5053;
	color: #ffffff;
	position: absolute;
	top: -6px;
	right: -4px;
	font-size: 12px;
}
</style>
